var CommentCon = iClass.get("Comment");
var UserCon = iClass.get("User");
var db = iClass.db;
var config = iClass.config;

iClass.method("Comment", {
	setInfo: function(info) {
		info.content = String(info.content || "").trim();
		/*
		 * 评论内容
		 */
		if (!info.content) {
			Throw("评论内容不可为空");
		}
		if (info.content.length > config.commentMaxLength) {
			Throw("评论内容长度超过限制（" + config.blogTitleMaxLength + "字以内）");
		}
		this.content = info.content;

		this.last_update_time = +new Date;
		this.save();
	},
	//获取详情
	getDetail: function(visitor_id) {
		var result = this.clone();
		//评论者详情
		var CommenterCon = CommentCon.CommenterTypeMap[this.type];
		result.commenter = CommenterCon.get(this.commenter_id);

		//获取子评论
		result.comments = db.find_list(this.dbname, {
			parent_id: this._id
		}).reverse().map(function(comment) {
			return CommentCon.getInstance(comment).getDetail(visitor_id);
		});
		//是否点过赞
		result._is_like = (Array.isArray(this.like_num) ? this.like_num : []).indexOf(visitor_id) !== -1
		// //转为纯数字
		// result.like_num = Array.isArray(this.like_num) ? result.like_num.length : 0;
		return result;
	},
	//非安全，点赞
	_like: function(user_id) {
		var like_num = this.like_num;
		if (!Array.isArray(like_num)) {
			like_num = this.like_num = [];
		}
		if (like_num.indexOf(user_id) !== -1) {
			Throw("用户已经点赞过")
		}
		like_num.unshift(user_id);
		this.save();
	},
	//非安全，取消赞
	_unlike: function(user_id) {
		var like_num = this.like_num;
		if (!Array.isArray(like_num)) {
			like_num = this.like_num = [];
		}
		var index = like_num.indexOf(user_id);
		if (index === -1) {
			Throw("用户未点过赞")
		}
		like_num.splice(index, 1);
		this.save();
	},
});